package com.hongxun.design.adapters;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.hongxun.design.R;
import com.hongxun.design.activitys.BaseActivity;
import com.hongxun.design.activitys.EncyclopediasActivity;
import com.hongxun.design.bean.EncyclopediaBean;
import com.hongxun.design.listeners.MClickListener;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.IntentToActivity;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.SetTypeface;
import com.hongxun.design.utils.TimeUtil;
import com.hongxun.design.utils.imageutil.UriUtil;

import java.util.List;

/**
 * Created by zerdoor_pc on 2016/6/20.
 */
public class EncyclopediasAdapter extends BaseAdapter {
    Context context;
    List<EncyclopediaBean> itemBeans;
    LayoutInflater inflater;

    public EncyclopediasAdapter(Context context, List<EncyclopediaBean> itemBeans) {
        this.context = context;
        this.itemBeans = itemBeans;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return itemBeans.size();
    }

    @Override
    public Object getItem(int i) {
        return itemBeans.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Holldler holldler = null;
        EncyclopediaBean bean= (EncyclopediaBean) getItem(i);
        if (view == null) {
            view = inflater.inflate(R.layout.listview_item_encyclopedias, null);
            holldler = new Holldler(view);
            view.setTag(holldler);
        } else {
            holldler = (Holldler) view.getTag();
        }

        try {
            holldler.name.setText(bean.title);
            holldler.image.setImageURI(UriUtil.getUri(bean.article_thumb));
            holldler.time.setText(TimeUtil.time(bean.create_time+"000"));
            holldler.item.setOnClickListener(new MClickListener(i,bean) {
                @Override
                public void onClick(View view, int position, Object msg) {
                    EncyclopediaBean b= (EncyclopediaBean) msg;
                    Bundle bundle=new Bundle();
                    bundle.putString(MString.CODE,b.encyclopedia_id);
                    bundle.putString(MString.TITLE,b.title);
                    if (b.favorite_id!=null&&b.favorite_id.length()>0){
                        bundle.putString(MString.SENDCODE,b.favorite_id);
                    }
                    bundle.putString(MString.TYPE, MString.Encyclopedias);
                    IntentToActivity.intent((BaseActivity)context, EncyclopediasActivity.class,bundle);
                }
            });
        }catch (Exception e){}

        return view;
    }


    class Holldler {
        SimpleDraweeView image;
        RelativeLayout item;
        TextView right, name, time;

        Holldler(View v) {
            item = (RelativeLayout) v.findViewById(R.id.encyclopedias_item);
            image = (SimpleDraweeView) v.findViewById(R.id.encyclopedias_image);
            right = (TextView) v.findViewById(R.id.encyclopedias_right);
            name = (TextView) v.findViewById(R.id.encyclopedias_name);
            time = (TextView) v.findViewById(R.id.encyclopedias_time);
            SetTypeface.set(right, IconSourse.RIGHTARROW);
        }
    }
}
